import { put, take, call, fork } from 'redux-saga/effects';

import * as types from '../constants/ActionTypes';
import ToastUtil from '../utils/ToastUtil';
import RequestXhrUtil from '../utils/RequestXhrUtil';
import { UA } from '../utils/AttachParamsUtil';
import { WEXIN_TEAM_LIST, SCRM_API } from '../constants/Urls';

import { requestSiteUser } from '../actions/siteUser';

export function* requestSiteUser(
  token,
  site_id,
  format,
  isRefreshing,
  loading
) {
  try {
    yield put(fetchSiteUser(isRefreshing, loading, isLoadMore));
    const SiteUser = yield call(
      RequestXhrUtil.request,
      `${SCRM_API}/users/${token}/site_users?site_id=${site_id}&format=${format}`,
      'get'
    );
    yield put(receiveSiteUser(
      SiteUser.results,
      false
    ));
    const errorMessage = SiteUser.errmsg;
    if (SiteUser.errcode && errorMessage !== '') {
      yield ToastUtil.showShort(errorMessage);
    }
  } catch (error) {
    yield put(receiveSiteUser([], false));
    ToastUtil.showShort('网络发生错误，请重试');
  }
}


export function* watchRequestSiteUser() {
  while (true) {
    const {
      token, site_id, format, isRefreshing, loading
    } = yield take(types.REQUEST_SITE_USER);
    yield fork(
      requestSiteUser,
      token,
      site_id,
      format,
      isRefreshing,
      loading
    );
  }
}